package com.atguigu.test

import scala.io.Source

  object Test2Again {
  /*
  1、统计哪些省份没有农产品市场
  2、统计农产品种类数最多的三个省份
  3、统计每个省份农产品种类数最多的三个农贸市场
   */
  def main(args: Array[String]): Unit = {
    //读取文件
    val allprovince = Source.fromFile("datas//allprovince.txt").getLines().toList
    val product = Source.fromFile("datas//product.txt").getLines().toList
    provinceNoMarket(allprovince,product)
    maxThreeProvince(product)
    provinceThreeMarket(product)
  }

    /**
     * 1.统计哪些省份没有农产品市场
      */
    def provinceNoMarket(allprovince:List[String],product:List[String])={
      //1.过滤脏数据
      val distinctlist = product.filter(_.split("\t").length == 6)
        //2.切分列 省份
        .map(_.split("\t")(4))
        //3.去重省份
        .distinct
      //4.差集
      allprovince.diff(distinctlist)
      //5.结果展现
        .foreach(println)
    }
    //2、统计农产品种类数最多的三个省份
    def maxThreeProvince(product:List[String])={
      //1.过滤脏数据
      product.filter(_.split("\t").length==6)
      //2.列裁剪(province,name)
        .map(line=>(line.split("\t")(4),line.split("\t")(0)))
      //3.(province,name)去重
        .distinct
      //4.province分组  province->list( (province,name) ,(),())
        .groupBy{
          case (province,name) => province
        }
      //5.统计各省份种类数
        .map{
          case (province,list) => (province,list.length)
        }
      //6.排序
        .toList
        .sortBy{
          case (province,num) => num
        }
        .reverse
      //7.取三个省份
        .take(3)
        .foreach(println)
    }
    //3、统计每个省份农产品种类数最多的三个农贸市场
    def provinceThreeMarket(product:List[String])={
      //1.过滤脏数据
      product.filter(_.split("\t").length==6)
      //2.列裁剪(province,market,name)
        .map(line=>(line.split("\t")(4),line.split("\t")(3),line.split("\t")(0)))
      //3.去重
        .distinct
      //4.province,market分组求出农产品种类
        .groupBy{
          case (province,market,name) =>(province,market)
        }
      //5.统计每个省每个市场农产品有多少种
        .map{
          case ((province,market),list) => (province,market,list.length)
        }
      //5.province分组排序各省各市场农产品种类取前三
        .groupBy{
          case (province,market,num) =>province
        }
        .map(x=>{
          x._2.toList.sortBy(_._3).reverse.take(3)
        })
        .foreach(println)
    }

}
